#!/usr/bin/python

#Imports
import os

errors = []

################################################################################
############################### COUNT ALL FILES ################################
################################################################################

def count_total_bytes(folders, recursive, ignore_folders):	
	files_total_bytes = 0
	while len(folders) > 0 :
		folder = folders.pop(0)
		try :
			files=os.listdir(folder)
		except (IOError, os.error), why : #catch the Error
			errors.append((folder + filename, folder + filename, str(why)))	
		except Error, err :
			errors.extend(err.args[0])
		except :
			print "Unexpected error:", sys.exc_info()[0]

		for filename in files :
			if filename != ".backup" :
				if os.path.isfile(folder + filename) and ( not os.path.islink(folder + filename) ):
	#				files_total += 1
					files_total_bytes += os.stat(folder + filename).st_size
				elif os.path.isdir(folder + filename) and ( not os.path.islink(folder + filename) ) and recursive == "1":
					try :
						i = ignore_folders.index(folder + filename + "/")
						ignore_folders.pop(i)
					except ValueError :
						folders.append(folder + filename + "/")
	return files_total_bytes
